{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "'''very WIP, should work fine\n",
    "requires iPyWidgets to be installed and enabled for jupyter\n",
    "see https://stackoverflow.com/questions/36351109/ipython-notebook-ipywidgets-does-not-show\n",
    "\n",
    "Lock on read may nor may not be needed (may need to acquire lock before changing CW settings/arming/capturing/etc)\n",
    "Current misses some chars, may need to reduce time.sleep() in read loop\n",
    "Call console.stop() to stop'''\n",
    "\n",
    "import ipywidgets as widgets\n",
    "import threading\n",
    "import time\n",
    "import ipywidgets as widgets\n",
    "from IPython.display import display\n",
    "\n",
    "text = widgets.Textarea(\n",
    "    value='Console',\n",
    "    placeholder='Paste ticket description here!',\n",
    "    description='String:',\n",
    "    disabled=False\n",
    ")\n",
    "display(text)\n",
    "\n",
    "class cw_console(object):\n",
    "    def __init__(self, ser):\n",
    "        self.serial = ser\n",
    "        self.quit = False\n",
    "        self.lock = threading.Lock()\n",
    "        \n",
    "    def run(self):\n",
    "        print(\"Starting\")\n",
    "        while not self.quit:\n",
    "            if self.lock.acquire(False): #lock maybe not necessary\n",
    "                # get num_chars waiting\n",
    "                waiting = self.serial.inWaiting()\n",
    "                if waiting:\n",
    "                    text.value=self.serial.read(waiting, timeout = 10)+text.value\n",
    "                time.sleep(0.1)\n",
    "                self.lock.release()\n",
    "                \n",
    "    def stop(self):\n",
    "        self.quit = True\n",
    "\n",
    "\n",
    "console = cw_console(target.ser)\n",
    "t = threading.Thread(target = console.run)\n",
    "t.start()"
   ]
  }
 ],
 "metadata": {
  "language_info": {
   "name": "python",
   "pygments_lexer": "ipython3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
